package com.simpletaskapp.persistence;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.simpletaskapp.domain.Task;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TaskDAO {

	private SQLiteDatabase database;
	private TaskSQLiteHelper dateBaseHelper;

	private SimpleDateFormat dateFormat = new SimpleDateFormat();

	public TaskDAO(Context context) {
		dateBaseHelper = new TaskSQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dateBaseHelper.getWritableDatabase();
	}

	public void close() {
		dateBaseHelper.close();
	}

	public Task saveOrUpdateTask(Task task) {
		ContentValues values = new ContentValues();
		
		String creationDate = dateFormat.format(task.getCreationDate());
		Integer isDone = task.isDone() ? 1 : 0;
		
		values.put(TaskColumns.DESCRIPTION_COLUMN.getName(), task.getDescription());
		values.put(TaskColumns.CREATION_DATE_COLUMN.getName(), creationDate);
		values.put(TaskColumns.DONE_COLUMN.getName(), isDone);
		
		Long id = task.getId();
		
		if (id == null)		
			id = database.insert(TaskSQLiteHelper.TASK_TABLE, null, values);
		else
			database.update(TaskSQLiteHelper.TASK_TABLE, values, TaskColumns.ID_COLUMN.getName() 
					+ " = " + id, null);
		
		Cursor cursor = database.query(TaskSQLiteHelper.TASK_TABLE,
				TaskColumns.getAllColumns(), 
				TaskColumns.ID_COLUMN.getName() + " = " + id, 
				null, null, null, null);
		
		cursor.moveToFirst();
		task = cursorToTask(task, cursor);
		cursor.close();
		
		return task;
	}

	public void deleteTask(Task task) {
		Long id = task.getId();
		database.delete(TaskSQLiteHelper.TASK_TABLE, TaskColumns.ID_COLUMN.getName() 
				+ " = " + id, null);
	}

	public List<Task> getAllTasks() {
		List<Task> tasks = new ArrayList<Task>();

		Cursor cursor = getAllTasksCursor();

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Task task = cursorToTask(cursor);
			tasks.add(task);
			cursor.moveToNext();
		}

		cursor.close();
		return tasks;
	}
	
	public Cursor getAllTasksCursor() {
		return database.query(TaskSQLiteHelper.TASK_TABLE,
				TaskColumns.getAllColumns(), null, null, null, null, null);
	}

	private Task cursorToTask(Cursor cursor) {
		Task task = new Task();
		return cursorToTask(task, cursor);
	}

	private Task cursorToTask(Task task, Cursor cursor) {
		try {
			Long id = cursor.getLong(0);
			String description = cursor.getString(1);
			String creationDate = cursor.getString(2);
			Integer done = cursor.getInt(3);

			task.setId(id);
			task.setDescription(description);
			task.setCreationDate(dateFormat.parse(creationDate));
			task.setDone(done.intValue() != 0);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}

		return task;
	}

	public void deletePerformedTasks() {
		database.delete(TaskSQLiteHelper.TASK_TABLE,
				TaskColumns.DONE_COLUMN.getName() + " = 1", null);
	}
}
